package com.dvj.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.dvj.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118604771";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCepxaWgns7vUEev2kSOQbfWlGEdJi48Ar55tT53C5ibarRMh55O4+khxGu/IQGOuc0jYMtvGYuvhSO5VOv3+uQ9/9t3bzbrLTNzEJxHvsxVwwDbvCFz2lQ1jdZkvZgTRd1bGGGm5GHJmNkQ7VP8oriLjnu4ZIlFEGePkHiqRBYmgle9pDJER9VA6gEtBVOi9SWwYZnvcui0CP/aceGbWyKk7731QVKvWER+VlzNTH5ybtMlTmEg7/0dYU98XHwjCrxgqngyQaSsQHnody0VJhfJi2FEK5Adgb7c9sH6kPm9SfJ5AvIcKQmFChiH6thnJCIUBVH/BXdXnci7Uzkdf4FAgMBAAECggEBAJ5wjV1H95vkV+lGDL93FgbuYuRdsLtYdg212dGhcu03SlTasZYuxjfv79gKNCsongdcn7mIIZ/ikhNWsGm+vLktAfkNbXMQmgvARkZEQ2W14bh4DVoElc917xbUbpojwx6wWwEb0Yzf6HiUKbkqpkpVEeQff1zqStGmqROb9UgVDWFiZ8KVYaTEGrIhteqhm7/zdLdkZJmnMFXVIKK9D1YqzyWk6QjLVO2PWE0KjU14rpP0Lml4uRCVEsTuSKBNZq5MSme0tnKPbJYaKAnyob9+eurMnExDn8+WwLbgkpagit+oNMbs8oag2R05qYxmTQt5uudRmRctCzVbUC+PNq0CgYEA7AOluvUV+H3YasKP9GiVH/Wv6Xp+Ui8I2uPoxZbM2RflrsAhpVVw05VgUGc2OQMsuDTeoHABBK+49eWSVTgnsQ4APogS+4J2y1L9uX5u2WsfzdX7yKpjJGiBfV6HsXYUPyFSYqoRU7Fmw3vRUZSdsK6o7ayhA6aeL1cr0eCqVksCgYEArBZin7qbYxF9T7ArHTlfht71cK9dVZuNA1DDpR03X57W7Mo1G15I3YLm56ZCi5Vq/wuzwb+txFuFKir7ols4yQ63IY6jQIYfaFMFGAT9dFCIlYYZcQiW4VtR5dMHcAD5v5lqblpE8gsEHKCr6NKJ6KfedgX0zAwhqYGPvpHhiu8CgYEA25ItHnkjHpKdw/0MhlrB2gDtzq5KVVUyzMnZ5fF81L/ZzDEtXC7YD9BZ0ZOkK3hbo2h0chihqeV9ky8oa6YZrywSaBqaZmki8dhc+rS4lgeGiC36195E7DtZtPQ8E4qCHZV3mxkTk6RosZdAzsAP0K89tBi1l7rsUaisuM5mSm8CgYEAhQqtZsXTGg1Mbw5I1sgjZVb8NAkvxF6E73pCN2XWCJP5n4lY83a6KJ7qO09bhSSYWuPGLlUtV3BWDFq8vjF+PlqlFEl9RwYqQ/JjLsW02XblkYviIXQ9ia6bK24yAtKOQyWtlfEt+l6v6B4DqfUTb45Qu7gxIIuEftLOuct4rnsCgYBThVp8pVXQmUb9N+haCVOQZi1H+dO9MNYjnDVuhjjmMaaXQwMbLM4n9VsTjglRnYIVkHnMljLu+Fbw+EouZj6/ORPfg9THFgQvTdmpTzRDRS1vePIcQqJyV8lKSNkbOKlwkNV7fDVQAvDtV91l8eIHHo0QQNi7sSEBvaaF/Ai1GA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgpj8Dz5F1toRTpJcaeMeQtpzES8NRxLvreNH5tVEuEjBaMs7/VG4I0gw3N+LicCOxJ7896ETLZQBOTMY0rxSDzFutFSKi/muuLjh829czQPfLDlGmmPOGFzGugvnd6Z8M7y56w3o0vNS3Z/wUx6yaI1tfZYLZD0FFafA2/EjeWx5NiKOG5o45Cx1f1z9SHIzRw4ulUjz2sqmKojiZrP1rLRp1dKiVHLBa7mQ8/boUuVG6lNZrcjINk9eq44+rc7dIWV11vO9q3z7b003Wreqv0MsY2sxAnovpTQ68w4zmgLiYFV0/zxmBBpq7JtnVj0dlgfR1jOtcLk62kw7DLaz0QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://dvj.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
